/*
 * @Author: hide_stone 
 * @Date: 2017-02-06 17:05:48 
 * @Last Modified by: hide_stone
 * @Last Modified time: 2017-05-04 09:30:59
 */
/**
 * 表单验证模块
 */

var validateRules = {
    isEmpty: function (value, errorMsg) {// 是否为空
        if (value == '') {
            return errorMsg
        }
    },
    maxLength: function (value, length, errorMsg) {//最大长度
        if (value.length > length) {
            return errorMsg
        }
    },
    minLength: function (value, length, errorMsg) {//最小长度
        if (value.length < length) {
            return errorMsg
        }
    },
    isMobile: function (value, errorMsg) { //电话验证
        var reg = /^0?1[3|4|5|7|8][0-9]\d{8}$/;
        if (!reg.test(value)) {
            return errorMsg;
        }
    },
    isEqual: function (value1, value2, errorMsg) {//是否相等
        if (value1 !== value2) {
            return errorMsg;
        }
    },
    ispsw: function (value, errorMsg) {
        var reg = /^[^\u4e00-\u9fa5]+$/
        if (!reg.test(value)) {
            return errorMsg
        }
    }
}

var Validator = function () {
    this.cach = [];
}


Validator.prototype.add = function (dom, rules) {
    var self = this;
    for (var i = 0, rule; rule = rules[i++];) {
        (function (rule) {
            var rulesArr = rule.ruleName.split(':');
            var errorMsg = rule.errorMsg

            self.cach.push(function () {
                var ruleName = rulesArr.shift();
                rulesArr.unshift(dom.value);
                rulesArr.push(errorMsg)
                return validateRules[ruleName].apply(dom, rulesArr)
            })
        })(rule)
    }
}

Validator.prototype.start = function () {
    for (var i = 0, validatefc; validatefc = this.cach[i++];) {
        var errormsg = validatefc();
        if (errormsg) {
            return errormsg;
        }
    }
}

module.exports = Validator;

















